//
//  WmcLocationLabel.swift
//  YPWatermarkCamera
//
//  Created by 赵新 on 2022/4/12.
//

import UIKit

public extension WmcLocationLabel {
    enum Format: Int {
        /// 地点
        case location
        /// 经纬度
        case longitudLatitude
        /// 中文经纬度
        case cn_longitudLatitude
    }
}

// MARK: - [WmcLocationLabel]

open class WmcLocationLabel: WmcBaseSystemFieldLabel {
    // Open

    /// 显示格式
    open var format: Format = .location {
        didSet { showText() }
    }

    /// 配置
    override open func config() {
        text = "定位获取中..."

        // 获取一次
        updateLocation(WmcLocationManager.shared.poiStatus.value?.data)

        // 监听定位
        WmcLocationManager.shared.locationStatus.subscribe(onNext: { [weak self] model in
            guard let weakSelf = self,
                  let model = model,
                  !weakSelf.styleMode.isStatic else { return }
            weakSelf.locateState.accept(model.status)
            weakSelf.showText()
        }).disposed(by: rx.disposeBag)

        // 监听poi
        WmcLocationManager.shared.poiStatus.subscribe(onNext: { [weak self] model in
            guard let weakSelf = self,
                  let model = model,
                  !weakSelf.styleMode.isStatic else { return }
            weakSelf.updateLocation(model.data)
            weakSelf.poiState.accept(model.status)
            weakSelf.showText()
        }).disposed(by: rx.disposeBag)

    }

    // Internal

    /// 当前定位
    var currentLocation: WmcSelectAddressModel?

    var locateState: BehaviorRelay<WmcAmapLocationManager.Status> = .init(value: .fetching)
    var poiState: BehaviorRelay<WmcLocationManager.PoiStatus> = .init(value: .updatingPoi)

    /// 子类获取定位的方法
    func updateLocation(_ location: WmcSelectAddressModel?) {
        YPGCD.Main { [weak self] in
            guard let weakSelf = self else { return }
            weakSelf.currentLocation = location
            weakSelf.showText()
        }
    }

    /// 显示文字
    func showText() {
        if WmcAuthorizedManager.location().info.isAvailable {
            var str = ""
            if poiState.value == .updatePoiCompletion {
                guard let currentLocation = currentLocation else {
                    return
                }
                // 直接转format
                switch format {
                case .location:
                    if let addressPrefix = currentLocation.addressPrefix {
                        str.append("\(addressPrefix) · ")
                    }
                    str.append(currentLocation.poi.name)
                case .longitudLatitude:
                    if currentLocation.poi.location != nil {
                        str = "N \(currentLocation.poi.location.latitude)°, E \(currentLocation.poi.location.longitude)°"
                    } else {
                        str = "网络不佳无法获取定位"
                    }
                case .cn_longitudLatitude:
                    if currentLocation.poi.location != nil {
                        str = "北纬\(currentLocation.poi.location.latitude)东经\(currentLocation.poi.location.longitude)"
                    } else {
                        str = "网络不佳无法获取定位"
                    }
                }
                text = str
            } else {
                str = "定位获取中..."
                if locateState.value == .failure { // 定位失败
                    str = "定位获取失败"
                }
                if poiState.value == .updatePoiFailure { // poi 失败
                    str = "网络不佳无法获取定位"
                }
            }
            text = str
        } else {
            text = "未开启定位权限"
        }
    }
}
